<template>
  <div class="body">
    <el-dialog
      :title="titleFlag ? '修改居民' : '添加居民'"
      :visible.sync="show"
      width="40%"
      style="text-align: center"
      :before-close="close"
    >
      <div class="addbody">
        <el-form
          :model="ruleForm"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <div class="left">
            <div class="photo">
              <p>头像</p>
              <Single
                :checkStatus="checkStatus"
                :imgUrl="imgUrl"
                @uploadCheck="uploadCheck"
              />
            </div>

            <el-form-item label="楼栋" prop="buildingid">
              <CascaderSelect
                style="width: 100px"
                @changeBuildId="getBuildId"
              />
            </el-form-item>
            <el-form-item label="房间号" prop="houseid">
              <el-select
                v-model="ruleForm.houseid"
                placeholder="请选择活动区域"
              >
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="ruleForm.sex">
                <el-radio label="男" value="1"></el-radio>
                <el-radio label="女" value="2"></el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
          <div class="right">
            <el-form-item label="民族" prop="nation">
              <el-input v-model="ruleForm.nation"></el-input> </el-form-item
            ><el-form-item label="籍贯" prop="usernative">
              <el-input v-model="ruleForm.usernative"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="cardid">
              <el-input v-model="ruleForm.cardid"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
              <el-input v-model="ruleForm.mobile"></el-input>
            </el-form-item>
            <el-form-item label="住户类型" prop="usertype">
              <el-select
                v-model="ruleForm.usertype"
                placeholder="请选择活动区域"
              >
                <el-option label="业主" value="1"></el-option>
                <el-option label="租户" value="2"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="addNotice">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import CascaderSelect from "./../building/cascader.vue";
import { userinfoadd, residentdelete } from "../../api/user";
import Single from "../upload/single.vue";
export default {
  components: {
    CascaderSelect,
    Single,
  },
  props: {
    titleFlag: {
      type: Boolean,
      default: false,
    },
    show: {
      type: Boolean,
      default: false,
    },
    updateData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      ruleForm: {
        id: 0,
        photo: "",
        name: "",
        houseid: "",
        usertype: "",
        sex: "",
        mobile: "",
        usernative: "",
        cardid: "",
        nation: "",
      },
      imgUrl: "",
      checkStatus: false,
    };
  },
  methods: {
    changeImg() {
      this.imgUrl =
        this.$config.imgUrl + "2022/03/04/202203041437580551596.jpg";
    },
    uploadCheck(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return (this.checkStatus = isJPG && isLt2M);
    },
    getBuildId(id) {
      console.log("buildingid", id);
      this.formline.buildingid = id;
    },
    async addNotice() {
      let msg = !this.ruleForm.id ? "添加成功" : "修改成功";
      const { errCode } = await userinfoadd(this.ruleForm);
      if (errCode === 10000) {
        this.$message.success(msg);
        this.$emit("close", false, undefined, 1);
        this.$refs.ruleForm.resetFields();
      }
    },
    close() {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.$emit("close", false);
        })
        .catch((_) => {});
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  watch: {
    updateData(newVal) {
      this.ruleForm = newVal;
    },
  },
};
</script>

<style lang="less">
.left {
  padding-left: 10px;
  box-sizing: border-box;
  width: 50%;
}
.el-form {
  display: flex;
}
.photo {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  p {
    margin-left: 30px;
    margin-right: 50px;
  }
}
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 80px;
  height: 80px;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.block {
  text-align: center;
}
</style>